home *** CD-ROM | disk | FTP | other *** search
/ EuroCD 3 / EuroCD 3.iso / Emulators / v2600 / Source.lha / Source / keyboard.c < prev    next >
Encoding:
C/C++ Source or Header  |  1997-04-25  |  4.6 KB  |  223 lines

  1. /*****************************************************************************
  2.  
  3.    This file is part of Virtual 2600, the Atari 2600 Emulator
  4.    ==========================================================
  5.    
  6.    Copyright 1996 Alex Hornby. For contributions see the file CREDITS.
  7.  
  8.    This software is distributed under the terms of the GNU General Public
  9.    License. This is free software with ABSOLUTELY NO WARRANTY.
  10.    
  11.    See the file COPYING for details.
  12.    
  13.    $Id: keyboard.c,v 1.17 1996/08/28 14:32:34 ahornby Exp $
  14.  
  15.    Tweaked by Matthew Stroup for Amiga v2600, April 24, 1997.
  16.  
  17. ******************************************************************************/
  18.  
  19. /* The device independant keyboard and mouse interface. */
  20.  
  21. #include <stdlib.h>
  22. #include "options.h"
  23. #include "types.h"
  24. #include "address.h"
  25. #include "vmachine.h"
  26. #include "kmap.h"
  27. #include "realjoy.h"
  28. #include "keybdrv.h"
  29. #include "moudrv.h"
  30.  
  31. enum control {STICK, PADDLE, KEYPAD};
  32.  
  33. enum joydirs {JLEFT, JRIGHT, JUP, JDOWN, JFIRE};
  34. int joykeys[2][5]={ 
  35.   {kmapLARROW, kmapRARROW, kmapUARROW, kmapDARROW, kmapRIGHTALT},
  36.   {kmapA, kmapS, kmapW, kmapZ, kmapLEFTALT}};
  37.  
  38. int padkeys[2][4][3]={ { {kmap1,kmap2,kmap3}, 
  39.              {kmapQ, kmapW, kmapE},
  40.              {kmapA, kmapS, kmapD},
  41.              {kmapZ, kmapX, kmapD}},
  42.                { {kmap5,kmap6,kmap7},
  43.              {kmapT, kmapY, kmapU},
  44.              {kmapG, kmapH, kmapJ},
  45.              {kmapB, kmapN, kmapM}}};
  46.  
  47. int moudrv_x, moudrv_y, moudrv_but;
  48.  
  49. static int raw;
  50.  
  51. /* Stores the keycodes */
  52. int keymap[103];
  53.  
  54. /* Returns mouse position */
  55. int mouse_position (void)
  56. {
  57.   int res;
  58.   moudrv_read();
  59.   
  60.   if(base_opts.mousey==1)
  61.     res=(((moudrv_y)*320)/200);
  62.   else
  63.     res=moudrv_x;
  64.   return res;
  65. }
  66.  
  67. /* Returns mouse button state */
  68. int mouse_button (void)
  69. {
  70.   moudrv_read();
  71.   return moudrv_but;
  72. }
  73.  
  74. /* Read from the keypad */
  75. void read_keypad(int pad) 
  76. {
  77.   int i,row,col;
  78.   
  79.   for(i=0; i<4; i++) 
  80.     keypad[pad][i]=0xff;
  81.  
  82.   for(row=0;row<4;row++)
  83.     {
  84.       for(col=0;col<3;col++)
  85.     {
  86.       if (keybdrv_pressed(padkeys[pad][row][col])) 
  87.         keypad[pad][row]=col;
  88.     }
  89.     }
  90. }
  91.  
  92.  
  93. /* Read from the emulated joystick */
  94. void read_stick (void)
  95. {
  96.   UBYTE v[2];
  97.   int i,jres;
  98.  
  99.   v[0] = v[1] = 0x0f;
  100.  
  101.   for(i=0;i<2;i++)
  102.     {      
  103.       jres = get_realjoy (i);
  104.       if (keybdrv_pressed(joykeys[i][JUP]) || (jres & UJOYMASK))
  105.     v[i] &= 0x0E;
  106.       else if (keybdrv_pressed(joykeys[i][JDOWN]) || (jres & DJOYMASK))
  107.     v[i] &= 0x0D;
  108.       if (keybdrv_pressed(joykeys[i][JLEFT]) || (jres & LJOYMASK))
  109.     v[i] &= 0x0B;
  110.       else if (keybdrv_pressed(joykeys[i][JRIGHT]) || (jres & RJOYMASK))
  111.     v[i] &= 0x07;
  112.     }
  113.  
  114.   /* Swap if necessary */
  115.   if (base_opts.swap)
  116.     riotRead[SWCHA] = (v[1] << 4) | v[0];
  117.   else
  118.     riotRead[SWCHA] = (v[0] << 4) | v[1];
  119. }
  120.  
  121. /* Read from emulated joystick trigger */
  122. void read_trigger (void)
  123. {
  124.   int kr = 0x80, kl = 0x80, temp;
  125.   int jres;
  126.  
  127.   /* keyboard_update(); */
  128.   /* update_realjoy(); */
  129.  
  130.   /* Left Player */
  131.   jres = get_realjoy (1);
  132.   if (keybdrv_pressed(joykeys[1][JFIRE]) || (jres & B1JOYMASK))
  133.     kl = 0x00;
  134.  
  135.   /* Right Player */
  136.   jres = get_realjoy (0);
  137.   if (keybdrv_pressed(joykeys[0][JFIRE]) || (jres & B1JOYMASK))
  138.     kr = 0x00;
  139.  
  140.   /* Swap the buttons if necessary */
  141.   if (base_opts.swap)
  142.     {
  143.       temp = kl;
  144.       kl = kr;
  145.       kr = temp;
  146.     }
  147.  
  148.   /* The normal non-latched mode */
  149.   if (!(tiaWrite[VBLANK] & 0x40))
  150.     {
  151.       tiaRead[INPT4] = kr;
  152.       tiaRead[INPT5] = kl;
  153.     }
  154.   /* The latched mode */
  155.   else
  156.     {
  157.       if (kr == 0)
  158.     tiaRead[INPT4] = 0x00;
  159.       if (kl == 0)
  160.     tiaRead[INPT5] = 0x00;
  161.     }
  162. }
  163.  
  164. /* Reads the keyboard */
  165. void read_keyboard (void)
  166. {
  167.   if (raw)
  168.     {
  169.       keybdrv_update ();
  170.       moudrv_update ();
  171.     }
  172.   if (keybdrv_pressed(kmapF10))
  173.     exit (0);
  174. }
  175.  
  176. /* Read console switches */
  177. void read_console (void)
  178. {
  179.   riotRead[SWCHB] |= 0x03;
  180.   if (keybdrv_pressed(kmapF2))
  181.     riotRead[SWCHB] &= 0xFE;    /* Reset */
  182.   if (keybdrv_pressed(kmapF3))
  183.     riotRead[SWCHB] &= 0xFD;    /* Select */
  184.  
  185.   if (keybdrv_pressed(kmapF4))
  186.     {
  187.       if (riotRead[SWCHB] & 0x08)
  188.     /* BW */
  189.     riotRead[SWCHB] &= 0xF7;
  190.       else
  191.     /* Color */
  192.     riotRead[SWCHB] |= 0x08;
  193.       while (keybdrv_pressed(kmapF4))
  194.     keybdrv_update ();
  195.     }
  196.  
  197.   if (keybdrv_pressed(kmapF5))
  198.     riotRead[SWCHB] &= 0xBF;    /* P0 amateur */
  199.   else if (keybdrv_pressed(kmapF6))
  200.     riotRead[SWCHB] |= 0x40;    /* P0 pro */
  201.   if (keybdrv_pressed(kmapF7))
  202.     riotRead[SWCHB] &= 0x7f;    /* P1 amateur */
  203.   else if (keybdrv_pressed(kmapF8))
  204.     riotRead[SWCHB] |= 0x80;    /* P1 pro */
  205. }
  206.  
  207. /* Close the keyboard */
  208. void close_keyboard (void)
  209. {
  210.   moudrv_close();
  211.   keybdrv_close();
  212.   raw=0;
  213. }
  214.  
  215. /* Initialises the keyboard */
  216. void init_keyboard (void)
  217. {
  218.   keybdrv_init ();
  219.   keybdrv_setmap();
  220.   moudrv_init();
  221.   raw=1;
  222. }
  223.